package com.umakr.ax.common.service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.umakr.ax.common.model.UserDetails;
import com.umakr.ax.common.mapper.UserDetailsMapper;
import com.umakr.ax.core.base.BaseService;
import com.umakr.ax.utils.WebUtil;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户详情 服务实现类
 * </p>
 *
 * @author Umakr-Code-Generator
 * @since 2017-12-30
 */
@Service
public class UserDetailsService extends BaseService<UserDetailsMapper, UserDetails>{

    /**
     * 唐腾飞修改：查询条件 由 like 编程eq
     * @param searchValue
     * @return
     */
    public Object listWriterOff(String searchValue){
        EntityWrapper<UserDetails> en = new EntityWrapper<>();
        if(searchValue != null && !"".equals(searchValue)) {
            en.eq("nickname", searchValue);
            en.or();
            en.eq("user_phone", searchValue);
        }
        en.orderBy("realname");
        Page<UserDetails> userPage =  selectPage(WebUtil.getPage(),en);

        Page<Map<String,Object>> resultPage = WebUtil.getPage();
        List<Map<String,Object>> reList = Lists.newArrayList();
        for(UserDetails ud : userPage.getRecords()){
            Map<String,Object> map = Maps.newHashMap();
            map.put("id",ud.getUserId());
            map.put("realName",ud.getRealname()==null?"":ud.getRealname());
            map.put("nickName",ud.getNickname()==null?"":ud.getNickname());
            map.put("phone",ud.getUserPhone()==null?"":ud.getUserPhone());
            reList.add(map);
        }
        resultPage.setRecords(reList);
        resultPage.setTotal(userPage.getTotal());

        return resultPage;
    }

    public void delByUserId(Long userId) {
        baseMapper.deleteByUserId(userId);
    }
}
